<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.label" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新建
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="标题" min-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleUpdate(row)">{{ row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.clientetetNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handeUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status.value === 5 || row.status.value === 1" size="mini" @click="handleModifyStatus(row,6)">
            启用
          </el-button>
          <el-button v-if="row.status.value === 6" size="mini" type="danger" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value === 4 || row.status.value == 2 || row.status.value == 5" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.start" :limit.sync="listQuery.perpage" @pagination="getList" />
  </div>
</template>

<script>
import { getLabels, changeStatusByLabelId, removeLabelById } from '@/api/customer/label'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      listQuery: {
        start: 1,
        perpage: 10,
        label: undefined,
        sort: '+id'
      },
      list: null,
      total: 0,
      labelForm: {

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCreate() {
      this.$router.push({ path: 'label-add' })
    },
    handeUpdate(item) {
      this.$router.push({ path: 'label-add', query: { 'labelId': item.identity }})
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getLabels(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.start = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      const _this = this
      const formData = {
        'labelId': row.identity,
        'status': status
      }
      changeStatusByLabelId(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.listQuery.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确实要执行此操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const formData = {
          'labelId': row.identity
        }
        removeLabelById(formData).then(response => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'group' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
